<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="typeCode">
        <el-input
          v-model="queryParams.typeCode"
          placeholder="请输入类型代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="typePic">
        <el-input
          v-model="queryParams.typePic"
          placeholder="请输入分类图像"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="auditState">
        <el-select v-model="queryParams.auditState" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="updateTimes">
        <el-date-picker
          v-model="queryParams.updateTimes"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="更新时间"
          end-placeholder="更新时间"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cms:articleType:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cms:articleType:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleAudit"
          v-hasPermi="['cms:articleType:audit']"
          >审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleUnaudit"
          v-hasPermi="['cms:articleType:audit']"
          >反审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleRemove"
          v-hasPermi="['cms:articleType:delete']"
          >回收站
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleRecover"
          v-hasPermi="['cms:articleType:delete']"
          >还原
        </el-button>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['cms:articleType:delete']"
            >删除
          </el-button>
        </el-col>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cms:articleType:export']"
          >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="分类名称" align="center" prop="typeName" />
      <el-table-column label="类型代码" align="center" prop="typeCode" />
      <el-table-column label="分类图像" align="center" prop="typePic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.typePic" :width="36" :height="36">
            <div slot="error" class="image-slot">
              <i class="el-icon-collection"></i>
            </div>
          </image-preview>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditState" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_audit_status" :value="scope.row.auditState" />
        </template>
      </el-table-column>
      <el-table-column label="开关状态" align="center" prop="switchState" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_switch_status" :value="scope.row.switchState" />
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" width="100" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="230px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="!canEdit(scope.row)"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cms:articleType:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['cms:articleType:audit']"
            >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUnaudit(scope.row)"
            v-hasPermi="['cms:articleType:audit']"
            >反审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            :disabled="!canDelete(scope.row)"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cms:articleType:delete']"
            >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRemove(scope.row)"
            v-hasPermi="['cms:articleType:delete']"
            >回收站
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRecover(scope.row)"
            v-hasPermi="['cms:articleType:delete']"
            >还原
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分类信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="类型代码" prop="typeCode">
          <el-input v-model="form.typeCode" placeholder="请输入类型代码" />
        </el-form-item>
        <el-form-item label="分类图像">
          <imageUpload v-model="form.typePic" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { canDelete, canEdit } from '@/utils/permission';
import {
  pageList,
  queryById,
  deleteByIds,
  insert,
  updateById,
  pageRelease,
  pageRecycle,
  auditByIds,
  unauditByIds,
  recoverByIds,
  removeByIds,
} from '@/api/cms/articleType';

export default {
  name: 'ArticleType',
  dicts: ['sys_delete_status', 'sys_audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageSize: 20,
        typeName: null,
        typeCode: null,
        typePic: null,
        auditState: null,
        switchState: null,
        updateTimes: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeName: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
        typeCode: [{ required: true, message: '类型代码不能为空', trigger: 'blur' }],
        auditState: [{ required: true, message: '审核状态不能为空', trigger: 'blur' }],
        deleted: [{ required: true, message: '删除状态不能为空', trigger: 'blur' }],
        editState: [{ required: true, message: '编辑状态不能为空', trigger: 'blur' }],
        switchState: [{ required: true, message: '开关状态不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    canDelete,
    canEdit,
    /** 查询分类信息列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then((resultDTO) => {
        this.list = resultDTO.data?.list;
        this.total = resultDTO.data?.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeName: null,
        typeCode: null,
        typePic: null,
        auditState: 0,
        deleted: 0,
        editState: 0,
        switchState: 0,
        updateBy: null,
        updateTime: null,
        createBy: null,
        createTime: null,
        auditBy: null,
        auditTime: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selecteds = selection;
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加分类信息';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (!row.id) {
        if (this.selecteds.some((item) => item.editState > 1)) {
          this.$modal.msgWarning('系统数据不能编辑');
          return;
        }
        if (this.selecteds.some((item) => item.auditState > 0)) {
          this.$modal.msgWarning('已审核数据不能编辑');
          return;
        }
      }
      this.reset();
      const id = row.id || this.ids;
      queryById(id).then((resultDTO) => {
        this.form = resultDTO.data;
        this.open = true;
        this.title = '修改分类信息';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateById(this.form).then((resultDTO) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            insert(this.form).then((resultDTO) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否审核分类信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return auditByIds(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('审核成功');
        })
        .catch(() => {});
    },
    /** 反审核按钮操作 */
    handleUnaudit(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否反审核分类信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return unauditByIds(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('反审核成功');
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (!row.id) {
        if (this.selecteds.some((item) => item.editState > 0)) {
          this.$modal.msgWarning('系统数据不能删除');
          return;
        }
        if (this.selecteds.some((item) => item.auditState > 0)) {
          this.$modal.msgWarning('已审核数据不能删除');
          return;
        }
      }
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除分类信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return deleteByIds(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 逻辑删除按钮操作 */
    handleRemove(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认逻辑删除分类信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return removeByIds(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 还原删除按钮操作 */
    handleRecover(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认还原删除分类信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return recoverByIds(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('还原成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'cms/articleType/export',
        {
          ...this.queryParams,
        },
        `articleType_${new Date().getTime()}.xlsx`,
      );
    },
  },
};
</script>
